import { ref } from "vue";
import { defineStore } from "pinia";
import { login } from "@/api/index.js";
import LocalCache from "@/utils/cache.js";
import { ElMessage } from "element-plus";
import {
  getUserListApi,
  createUserApi,
  updateUserApi,
  deleteUserApi,
  updateUserPasswordByIdApi,
} from "@/api/user";

import { getMenuTreeApi } from "@/api/menu";

import { menuTree } from "@/utils/menuTree";

import router from "@/router";

export const useUserStore = defineStore(
  "user",
  () => {
    const userInfo = ref({});
    const role = ref({});
    const userMenuList = ref([]);

    async function getUserMenuTree() {
      const res = await getMenuTreeApi();
      if (res.data.code === 200) {
        userMenuList.value = menuTree(res.data.data);
        localStorage.setItem(
          "dynamicRoutes",
          JSON.stringify(userMenuList.value)
        );
        return userMenuList.value;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    function reloadingMenuTree() {
      return menuTree(userMenuList.value);
    }

    async function getUserInfo(account, password) {
      const res = await login(account, password);
      if (res.data.code === 200) {
        userInfo.value = res.data.data.user;
        role.value = res.data.data.role;
        LocalCache.setCache("token", res.data.data.token);
        return true;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    function logout() {
      LocalCache.clearCache();
      userInfo.value = {};
      role.value = {};
      userMenuList.value = [];
      LocalCache.clearCache();

      router.push("/login");
    }

    async function getUserList(currentPage, pageSize) {
      const res = await getUserListApi(currentPage, pageSize);
      if (res.data.code === 200) {
        return res.data.data;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    async function createUser(userObj) {
      const res = await createUserApi(userObj);
      if (res.data.code === 200) {
        return true;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    async function updateUser(id, userObj) {
      const res = await updateUserApi(id, userObj);
      if (res.data.code === 200) {
        return true;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    async function deleteUser(id) {
      const res = await deleteUserApi(id);
      if (res.data.code === 200) {
        return true;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    async function updateUserPasswordById(id, password, newPassword) {
      const res = await updateUserPasswordByIdApi(id, password, newPassword);
      if (res.data.code === 200) {
        return true;
      } else {
        ElMessage.error({
          message: res.data.msg,
        });
        return false;
      }
    }

    return {
      userInfo,
      role,
      userMenuList,
      getUserMenuTree,
      reloadingMenuTree,
      getUserInfo,
      logout,
      getUserList,
      createUser,
      updateUser,
      deleteUser,
      updateUserPasswordById,
    };
  },
  {
    persist: {
      storage: sessionStorage,
      paths: ["userInfo", "role", "userMenuList"],
    },
  }
);

export function useUserStoreHook() {
  const userStore = useUserStore();
  return userStore;
}
